package cn.iocoder.yudao.module.ao.controller.admin.classdiscipline;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.classdiscipline.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.classdiscipline.ClassDisciplineDO;
import cn.iocoder.yudao.module.ao.service.classdiscipline.ClassDisciplineService;

@Tag(name = "管理后台 - 高技课堂纪律情况登记")
@RestController
@RequestMapping("/ao/class-discipline")
@Validated
public class ClassDisciplineController {

    @Resource
    private ClassDisciplineService classDisciplineService;

    @PostMapping("/create")
    @Operation(summary = "创建高技课堂纪律情况登记")
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:create')")
    public CommonResult<Long> createClassDiscipline(@Valid @RequestBody ClassDisciplineSaveReqVO createReqVO) {
        return success(classDisciplineService.createClassDiscipline(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新高技课堂纪律情况登记")
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:update')")
    public CommonResult<Boolean> updateClassDiscipline(@Valid @RequestBody ClassDisciplineSaveReqVO updateReqVO) {
        classDisciplineService.updateClassDiscipline(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除高技课堂纪律情况登记")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:delete')")
    public CommonResult<Boolean> deleteClassDiscipline(@RequestParam("id") Long id) {
        classDisciplineService.deleteClassDiscipline(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除高技课堂纪律情况登记")
                @PreAuthorize("@ss.hasPermission('ao:class-discipline:delete')")
    public CommonResult<Boolean> deleteClassDisciplineList(@RequestParam("ids") List<Long> ids) {
        classDisciplineService.deleteClassDisciplineListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得高技课堂纪律情况登记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:query')")
    public CommonResult<ClassDisciplineRespVO> getClassDiscipline(@RequestParam("id") Long id) {
        ClassDisciplineDO classDiscipline = classDisciplineService.getClassDiscipline(id);
        return success(BeanUtils.toBean(classDiscipline, ClassDisciplineRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得高技课堂纪律情况登记分页")
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:query')")
    public CommonResult<PageResult<ClassDisciplineRespVO>> getClassDisciplinePage(@Valid ClassDisciplinePageReqVO pageReqVO) {
        PageResult<ClassDisciplineDO> pageResult = classDisciplineService.getClassDisciplinePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassDisciplineRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课堂纪律登记 Excel")
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassDisciplineExcel(@Valid ClassDisciplinePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassDisciplineDO> list = classDisciplineService.getClassDisciplinePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课堂纪律登记.xls", "数据", ClassDisciplineRespVO.class,
                        BeanUtils.toBean(list, ClassDisciplineRespVO.class));
    }

    @GetMapping("/export-statistics")
    @Operation(summary = "导出课堂纪律统计表 Excel")
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassDisciplineStatistics(@Valid ClassDisciplinePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        classDisciplineService.exportStatistics(pageReqVO, response);
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得课堂纪律登记导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手工创建导出 demo
        List<ClassDisciplineImportReqVO> list = Arrays.asList(
            ClassDisciplineImportReqVO.builder()
                .recordNumber("") // 记录编号可以为空，系统自动生成
                .fillTimeStr("2025-11-03 14:30:00")
                .className("计算机1班")
                .classSessionStr("1")
                .eveningSessionStr("1")
                .unbaggedPhonesStr("0")
                .absentCountStr("0")
                .specialAbsentCountStr("0")
                .isSaluteStr("是")
                .isRollCallStr("是")
                .isAssignShowHomeworkStr("是")
                .eveningHomeworkContent("完成数学作业第1-10题")
                .remark("课堂纪律良好")
                .imageUrl("")
                .processStatus("未处理")
                .filler("张老师")
                .build(),
            ClassDisciplineImportReqVO.builder()
                .recordNumber("")
                .fillTimeStr("2025-11-03 15:30:00")
                .className("计算机2班")
                .classSessionStr("2")
                .eveningSessionStr("")
                .unbaggedPhonesStr("1")
                .absentCountStr("2")
                .specialAbsentCountStr("1")
                .isSaluteStr("否")
                .isRollCallStr("是")
                .isAssignShowHomeworkStr("否")
                .eveningHomeworkContent("")
                .remark("有学生迟到")
                .imageUrl("")
                .processStatus("处理中")
                .filler("李老师")
                .build()
        );
        // 输出
        ExcelUtils.write(response, "课堂纪律登记导入模板.xls", "数据", ClassDisciplineImportReqVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入课堂纪律登记")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('ao:class-discipline:import')")
    public CommonResult<ClassDisciplineImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                                @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<ClassDisciplineImportReqVO> list = ExcelUtils.read(file, ClassDisciplineImportReqVO.class);
        return success(classDisciplineService.importClassDisciplineList(list, updateSupport));
    }

}